package com.website.system.controller;

import com.alibaba.fastjson.JSON;
import com.website.common.controller.BaseController;
import com.website.core.util.EChartsUtil;
import com.website.system.model.Visitor;
import com.website.system.service.ArticleService;
import com.website.system.service.VisitorService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * website项目后台
 *
 * @author yanqb
 * @date 2018-11-16 15:43
 */
@Controller
@RequestMapping("/manage")
public class ManageController extends BaseController {

    @Resource
    private ArticleService articleService;

    @Resource
    private VisitorService visitorService;

    @RequestMapping("/index")
    public String index(Model model) {
        model.addAttribute("currentUser", getCurrentUser());
        return "manage/index";
    }

    @RequestMapping("/main")
    public String main(Model model) {
        // 文章数量按月统计近12个月，文件数量按月统计近6个月，访问量按天统计近30天
        model.addAttribute("current_browse",BROWSE_COUNT);
        model.addAttribute("current_article",articleService.getArticleEasyForPicture().size());

        List<Visitor> browseList = visitorService.getBrowseCount();
        model.addAttribute("browse_xAxis",browseList.stream().map(Visitor::getDay).collect(Collectors.toList()));
        model.addAttribute("browseData",browseList.stream().map(Visitor::getBrowseCount).collect(Collectors.toList()));

        List<Visitor> downloadList = visitorService.getDownloadCount();
        model.addAttribute("download_xAxis", JSON.toJSONString(EChartsUtil.getChineseMonths(6)));
        model.addAttribute("downloadData",downloadList.stream().map(Visitor::getSum).collect(Collectors.toList()));

        List<Visitor> articleList = visitorService.getArticleCount();
        model.addAttribute("article_xAxis", JSON.toJSONString(EChartsUtil.getEnglishMonths(6)));
        model.addAttribute("articleData1", EChartsUtil.getArticleCountMap(articleList,1));
        model.addAttribute("articleData2", EChartsUtil.getArticleCountMap(articleList,2));
        model.addAttribute("articleData3", EChartsUtil.getArticleCountMap(articleList,3));

        return "manage/main";
    }

}
